/**
 * 通知封装方法， 需要安装以下插件
   DingTalk 2.4.3
   Slack Notification 2.45
   Config File Provider Plugin 3.8.2
   以上版本测试通过
   邮件模板会拉取定义好的配置文件
   配置地址：/configfiles/editConfig?id=email-template
 * @return
 */

/*
@Library('global-shared-library@master') _

pipeline {
    agent any

    environment {
       email_to='maf@shinetechchina.com'
       slack_channel='#personal'
       ding_robot='cages'
    }

    post {
        always {
            sendNotify()
        }
    }        
 }   
*/


def getTitle() {
    return "Job [${env.JOB_NAME}] -- Status: ${currentBuild.result?: 'success'} -- Build No: ${env.BUILD_NUMBER}"
}

def resultImage() {
    if (currentBuild.result == 'SUCCESS') {
        return 'https://mafeifan.pek3b.qingstor.com/Jenkins/icon/icon-success.jpeg'
    } else {
        return 'https://mafeifan.pek3b.qingstor.com/Jenkins/icon/icon-error.jpeg'
    }
}

def call(Map config) {
  script{
      if (params.p_notify == '钉钉' || default_notify == 'dingding') {
          dingtalk (
              robot: "${ding_robot}",
              type: 'LINK',
              title: "${currentBuild.result?: 'success'}",
              text: [
                getTitle(),
              ],
              messageUrl: "${env.BUILD_URL}console",
              picUrl: resultImage()
          )
      }
      else if (params.p_notify == 'Slack' || default_notify == 'slack') {
          slackSend channel: "${slack_channel}", message: getTitle()
      }
      else if (params.p_notify == '邮件' || default_notify == 'email') {
          configFileProvider([configFile(fileId: 'email-template', targetLocation: 'email.html', variable: 'content')]) {
              script {
                  template = readFile encoding: 'UTF-8', file: "${content}"
                  emailext(
                      to: "${email_to}",
                      subject: getTitle(),
                      body: """${template}"""
                  )
              }
          }
      }
  }
}
